$(function () {
    findById();
});

/**
 * 查询id
 */
function findById() {
    let id = sessionStorage.getItem("companyId");
    let result = myAjax("/admin/company/findById", {id: id}, "post");
    console.log(result);
    if (result != null) {
        setData(result.model);
    }

}

function setData(data) {
    $("#companyName").val(data.name);
    $("#showImg").attr("src", URL + data.logo);
    edit.txt.html(data.content)
    $("#companyPhone").val(data.phone);
    $("#companyEmail").val(data.email);
    $("#showQrCodeIndex").attr("src", URL + data.qrCodeIndex);
    $("#showQrCodeWeiXin").attr("src", URL + data.qrCodeWeiXin);
    $("#companyAddress").val(data.address);
    $("#online").val(data.online);
    $("#recordNumber").val(data.recordNumber);
    $("#recordMessage").val(data.recordMessage);
    $("input[name='status'][value=" + data.status + "]").attr("checked", true);
}


//返回用户查询结果页面
// function goBack() {
//     $('.right').load('/html/admin/company/companyManage.html');
// }
//提交表单时再一次判断提交数据是否正确
function save() {
    let data = new FormData();
    data.append("id", sessionStorage.getItem("companyId"))
    data.append("name", $("#companyName").val());
    data.append("online", $("#online").val());
    data.append("address", $("#companyAddress").val());
    data.append("phone", $("#companyPhone").val());
    data.append("email", $("#companyEmail").val());
    data.append("status", $("input:radio[name='status']:checked").val());
    //拿到的图片有可能是一组数据，[0].files[0]选中当前对象的第一个数据
    data.append("img", $("#logo")[0].files[0]);
    data.append("qrCodeIndex", $("#qrCodeIndex")[0].files[0]);
    data.append("qrCodeWeiXin", $("#qrCodeWeiXin")[0].files[0]);
    data.append("recordMessage", $("#recordMessage").val());
    data.append("recordNumber", $("#recordNumber").val());
    data.append("content", edit.txt.html());

    let result = myImgAjax("/adminFile/company/update", data, "post");
    console.log(result);
    if (result.result == "success") {
        alert("修改成功（== ^^ ==）！");
        parent.location.reload();
    } else if (result.result == "noLogin") {
        window.open("/html/login.html", "_self");
    } else {
        alert("修改失败！");
    }
}